package com.dsp.soundscape.util;

import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class ConfigLoader {
	private ConfigLoader(){}
	
	@SuppressWarnings("unchecked")
	public static <T> T load(String fileName, String packageName) throws JAXBException {
		JAXBContext jc = JAXBContext.newInstance( packageName );
		Unmarshaller u = jc.createUnmarshaller();
		JAXBElement<T> configuration = (JAXBElement<T>) u.unmarshal(getResource(fileName));
		return configuration.getValue();
	}
	
	public static URL getResource(String fileName) {
		URL resource = Thread.currentThread().getContextClassLoader().getResource(fileName);
		if (resource == null) {
			return ClassLoader.getSystemResource( fileName );
		}
		return resource;
	}
}
